package action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import bean.Country;
import bean.Destination;
import bean.Status;
import bean.User;
import bean.UserDestGrading;
import bean.UserDestGradingId;
import dao.CountryDAO;
import dao.DestinationDAO;
import dao.UserDAO;
import dao.UserDestGradingDAO;
import form.VoteForm;

public class GetDestVoteAction extends Action {
	

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	
    
    VoteForm score=new VoteForm(Integer.parseInt(request.getParameter("shopping")),
    		Integer.parseInt(request.getParameter("exploring")),
    				Integer.parseInt(request.getParameter("educational")),
    						Integer.parseInt(request.getParameter("relaxing")),Integer.parseInt(request.getParameter("historical")));
    UserDAO userDAO=new UserDAO();
	DestinationDAO destDAO=new DestinationDAO();
	CountryDAO countryDAO=new CountryDAO();
	UserDestGradingDAO udgDAO=new UserDestGradingDAO();
	ActionErrors errors=new ActionErrors();
	HttpSession session=request.getSession();
	UserDestGradingId udID=(UserDestGradingId)session.getAttribute("userDestID");
	System.out.print(udID.getUser().toString()+udID.getDestination().toString());
	
	if(udID!=null){
		Destination dest=udID.getDestination();
		Country temp=countryDAO.findById(dest.getCountry().getCountryId());
		dest.setCountry(temp);
		
		UserDestGrading newGrade=new UserDestGrading();
		newGrade.setId(udID);
		newGrade.setShopping(score.getShopping());
		newGrade.setEducational(score.getEducational());
		newGrade.setExploring(score.getExploring());
		newGrade.setRelaxing(score.getRelaxing());
		newGrade.setHistorical(score.getHistorical());
		
		udgDAO.merge(newGrade);
		//Set gradeSet=dest.getUserDestGradings();
		//gradeSet.add(newGrade);
		ArrayList glist=new ArrayList();
		glist=(ArrayList)udgDAO.findAll();
		
		int totalEduRate=0;
		int totalExpRate=0;
		int totalHisRate=0;
		int totalRelRate=0;
		int totalShpRate=0;
		
		for (Iterator it = glist.iterator(); it.hasNext(); ) {
			UserDestGrading s = (UserDestGrading)it.next();  // No downcasting required.
			UserDestGrading ns=udgDAO.findById(s.getId());
			totalEduRate+=ns.getEducational();
			totalExpRate+=ns.getExploring();
			totalHisRate+=ns.getHistorical();
			totalRelRate+=ns.getRelaxing();
			totalShpRate+=ns.getShopping();
		}
		int avgEduRate=(int)totalEduRate/glist.size();
		int avgExpRate=(int)totalExpRate/glist.size();
		int avgHisRate=(int)totalHisRate/glist.size();
		int avgRelRate=(int)totalRelRate/glist.size();
		int avgShpRate=(int)totalShpRate/glist.size();
		
		UserDestGrading avgRate=new UserDestGrading(avgShpRate,avgExpRate,avgEduRate,avgRelRate,avgHisRate);
		
		request.setAttribute("destination", dest);
		request.setAttribute("destRate", avgRate);
		return mapping.findForward("gotNewRate");
		}else{
		errors.add("notExist",new ActionMessage("Destination.notExist"));
		
		}
	    saveErrors(request, errors);
	    return mapping.getInputForward();
	}

}
